In [1]:
import pandas as pd
import numpy as np
import geopandas as gpd
In [2]:
beats=gpd.read_file("https://services.arcgis.com/ZOyb2t4B0UYuYNYH/arcgis/rest/services/Current_Beats/FeatureServer/0/query?outFields=*&where=1%3D1&f=geojson")
In [3]:
nbh=gpd.read_file("https://services.arcgis.com/ZOyb2t4B0UYuYNYH/arcgis/rest/services/nma_nhoods_sub/FeatureServer/0/query?outFields=*&where=1%3D1&f=geojson")
In [4]:
incidents = pd.read_json("https://data.seattle.gov/resource/tazs-3rd5.json?$query=SELECT%0A%20%20%60report_number%60%2C%0A%20%20%60report_date_time%60%2C%0A%20%20%60offense_id%60%2C%0A%20%20%60offense_date%60%2C%0A%20%20%60nibrs_group_a_b%60%2C%0A%20%20%60nibrs_crime_against_category%60%2C%0A%20%20%60offense_sub_category%60%2C%0A%20%20%60shooting_type_group%60%2C%0A%20%20%60block_address%60%2C%0A%20%20%60latitude%60%2C%0A%20%20%60longitude%60%2C%0A%20%20%60beat%60%2C%0A%20%20%60precinct%60%2C%0A%20%20%60sector%60%2C%0A%20%20%60neighborhood%60%2C%0A%20%20%60reporting_area%60%2C%0A%20%20%60offense_category%60%2C%0A%20%20%60nibrs_offense_code_description%60%2C%0A%20%20%60nibrs_offense_code%60%0AWHERE%0A%20%20%60report_date_time%60%0A%20%20%20%20BETWEEN%20%222025-11-03T00%3A00%3A00%22%20%3A%3A%20floating_timestamp%0A%20%20%20%20AND%20%222025-11-07T22%3A45%3A00%22%20%3A%3A%20floating_timestamp")
incidents
Out[4]:
| report_number | report_date_time | offense_id | offense_date | nibrs_group_a_b | nibrs_crime_against_category | offense_sub_category | shooting_type_group | block_address | latitude | longitude | beat | precinct | sector | neighborhood | reporting_area | offense_category | nibrs_offense_code_description | nibrs_offense_code | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 2025-321958 | 2025-11-03 00:20:01 | 67167585930 | 2025-11-02T23:12:00.000 | A | PERSON | ASSAULT OFFENSES | - | REDACTED | REDACTED | REDACTED | S2 | South | S | BRIGHTON/DUNLAP | REDACTED | ALL OTHER | Intimidation | 13C |
| 1 | 2025-321962 | 2025-11-03 00:58:02 | 67167929818 | 2025-11-02T23:22:00.000 | A | PERSON | VIOLATION OF NO CONTACT ORDER | - | REDACTED | REDACTED | REDACTED | S1 | South | S | MID BEACON HILL | REDACTED | ALL OTHER | Violation of No Contact Orders | 500 |
| 2 | 2025-321994 | 2025-11-03 01:05:38 | 67167876122 | 2025-11-03T00:25:00.000 | B | NOT_A_CRIME | 999 | - | 103XX BLOCK OF AURORA AVE N | 47.70342166 | -122.344674750346 | N3 | North | N | NORTHGATE | 1967 | ALL OTHER | Not Reportable to NIBRS | 999 |
| 3 | 2025-321990 | 2025-11-03 01:12:58 | 67167892721 | 2025-11-03T00:19:00.000 | A | PROPERTY | ROBBERY | - | 112XX BLOCK OF AURORA AVE N | 47.71119796 | -122.344795214832 | N3 | North | N | NORTHGATE | 11341 | VIOLENT CRIME | Robbery | 120 |
| 4 | 2025-321981 | 2025-11-03 01:33:07 | 67167946601 | 2025-11-03T00:01:00.000 | A | PERSON | ASSAULT OFFENSES | - | 29XX BLOCK OF S COLUMBIAN WAY | 47.56262374 | -122.295274743911 | R2 | South | R | CLAREMONT/RAINIER VISTA | 10168 | ALL OTHER | Simple Assault | 13B |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 992 | 2025-326543 | 2025-11-06 22:20:01 | 67242117613 | 2025-11-06T21:10:00.000 | B | ANY | ALL OTHER | - | 1XX BLOCK OF 6TH AVE S | 47.60129044 | -122.326352376011 | K3 | West | K | CHINATOWN/INTERNATIONAL DISTRICT | 822 | ALL OTHER | All Other Offenses | 90Z |
| 993 | 2025-326543 | 2025-11-06 22:20:01 | 67242080867 | 2025-11-06T21:10:00.000 | A | PERSON | ASSAULT OFFENSES | - | 1XX BLOCK OF 6TH AVE S | 47.60129044 | -122.326352376011 | K3 | West | K | CHINATOWN/INTERNATIONAL DISTRICT | 822 | ALL OTHER | Simple Assault | 13B |
| 994 | 2025-326526 | 2025-11-06 22:20:45 | 67242088334 | 2025-11-06T20:54:00.000 | A | PROPERTY | BURGLARY | - | 9XX BLOCK OF NW MARKET ST | 47.66866158 | -122.369682040098 | B2 | North | B | BALLARD SOUTH | 4700 | PROPERTY CRIME | Burglary/Breaking & Entering | 220 |
| 995 | 2025-326592 | 2025-11-06 22:51:55 | 67242211063 | 2025-11-06T22:30:00.000 | B | ANY | ALL OTHER | - | 1XX BLOCK OF BLANCHARD ST | 47.61293883 | -122.3447833 | D1 | West | D | BELLTOWN | 8886 | ALL OTHER | All Other Offenses | 90Z |
| 996 | 2025-326576 | 2025-11-06 23:00:37 | 67242247965 | 2025-11-06T22:00:00.000 | A | PERSON | ASSAULT OFFENSES | - | 1XX BLOCK OF NE 94TH ST | 47.69684659 | -122.327638372321 | L2 | North | L | - | 7455 | ALL OTHER | Simple Assault | 13B |
997 rows × 19 columns
In [5]:
beats.crs
beats.head()
Out[5]:
| objectid | beat | first_precinct | sector | Shape__Area | Shape__Length | geometry | |
|---|---|---|---|---|---|---|---|
| 0 | 1 | 99 | None | 99 | 2.087731e+08 | 280794.869337 | MULTIPOLYGON (((-122.38 47.749, -122.38 47.748... |
| 1 | 2 | B1 | N | B | 3.888917e+07 | 30766.974987 | POLYGON ((-122.41 47.676, -122.41 47.675, -122... |
| 2 | 3 | B2 | N | B | 5.514478e+07 | 32647.183200 | POLYGON ((-122.37 47.676, -122.37 47.676, -122... |
| 3 | 4 | B3 | N | B | 5.961233e+07 | 36973.284491 | POLYGON ((-122.34 47.669, -122.34 47.669, -122... |
| 4 | 5 | C1 | E | C | 3.255283e+07 | 37950.240764 | POLYGON ((-122.31 47.614, -122.31 47.614, -122... |
In [6]:
incidents.head()
Out[6]:
| report_number | report_date_time | offense_id | offense_date | nibrs_group_a_b | nibrs_crime_against_category | offense_sub_category | shooting_type_group | block_address | latitude | longitude | beat | precinct | sector | neighborhood | reporting_area | offense_category | nibrs_offense_code_description | nibrs_offense_code | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 2025-321958 | 2025-11-03 00:20:01 | 67167585930 | 2025-11-02T23:12:00.000 | A | PERSON | ASSAULT OFFENSES | - | REDACTED | REDACTED | REDACTED | S2 | South | S | BRIGHTON/DUNLAP | REDACTED | ALL OTHER | Intimidation | 13C |
| 1 | 2025-321962 | 2025-11-03 00:58:02 | 67167929818 | 2025-11-02T23:22:00.000 | A | PERSON | VIOLATION OF NO CONTACT ORDER | - | REDACTED | REDACTED | REDACTED | S1 | South | S | MID BEACON HILL | REDACTED | ALL OTHER | Violation of No Contact Orders | 500 |
| 2 | 2025-321994 | 2025-11-03 01:05:38 | 67167876122 | 2025-11-03T00:25:00.000 | B | NOT_A_CRIME | 999 | - | 103XX BLOCK OF AURORA AVE N | 47.70342166 | -122.344674750346 | N3 | North | N | NORTHGATE | 1967 | ALL OTHER | Not Reportable to NIBRS | 999 |
| 3 | 2025-321990 | 2025-11-03 01:12:58 | 67167892721 | 2025-11-03T00:19:00.000 | A | PROPERTY | ROBBERY | - | 112XX BLOCK OF AURORA AVE N | 47.71119796 | -122.344795214832 | N3 | North | N | NORTHGATE | 11341 | VIOLENT CRIME | Robbery | 120 |
| 4 | 2025-321981 | 2025-11-03 01:33:07 | 67167946601 | 2025-11-03T00:01:00.000 | A | PERSON | ASSAULT OFFENSES | - | 29XX BLOCK OF S COLUMBIAN WAY | 47.56262374 | -122.295274743911 | R2 | South | R | CLAREMONT/RAINIER VISTA | 10168 | ALL OTHER | Simple Assault | 13B |
In [7]:
nbh_sort = nbh.sort_values('Shape__Area', ascending = False)
print(nbh_sort['S_HOOD'])
93 Industrial District
23 Lawton Park
53 Mid-Beacon Hill
41 Georgetown
40 SODO
...
37 Yesler Terrace
92 Denny Triangle
70 Portage Bay
20 Westlake
32 Pike-Market
Name: S_HOOD, Length: 94, dtype: object
In [8]:
len(beats)
Out[8]:
55
In [9]:
incidents['longitude'].describe()
Out[9]:
count 997 unique 719 top REDACTED freq 112 Name: longitude, dtype: object
In [10]:
incidents['latitude'].describe()
Out[10]:
count 997 unique 696 top REDACTED freq 112 Name: latitude, dtype: object
In [11]:
incidents2 = incidents[(incidents['longitude'] != 'REDACTED')]
In [12]:
incidents2 = incidents2.astype({'longitude': float, 'latitude':float})
In [13]:
# Geopandas.GeoDataFrame() method will conver the data to spatial form, Geo dataframe with points_from_xy method.
spd_crime=gpd.GeoDataFrame(incidents2, geometry=gpd.points_from_xy(incidents2.longitude, incidents2.latitude), crs='EPSG:4326')
In [14]:
spd_crime.plot()
Out[14]:
<Axes: >
In [15]:
spd_crime['longitude'].describe()
Out[15]:
count 885.000000 mean -121.097621 std 12.180086 min -122.411979 25% -122.349529 50% -122.328110 75% -122.312761 max -1.000000 Name: longitude, dtype: float64
In [16]:
spd_crime2 = spd_crime[(spd_crime['longitude'] != -1)]
In [17]:
spd_crime2.plot()
Out[17]:
<Axes: >
In [18]:
spd_crime3 = spd_crime2[[ 'report_number','report_date_time','offense_id','offense_category' ,'latitude', 'longitude','geometry']]
spd_crime3
Out[18]:
| report_number | report_date_time | offense_id | offense_category | latitude | longitude | geometry | |
|---|---|---|---|---|---|---|---|
| 2 | 2025-321994 | 2025-11-03 01:05:38 | 67167876122 | ALL OTHER | 47.703422 | -122.344675 | POINT (-122.34 47.703) |
| 3 | 2025-321990 | 2025-11-03 01:12:58 | 67167892721 | VIOLENT CRIME | 47.711198 | -122.344795 | POINT (-122.34 47.711) |
| 4 | 2025-321981 | 2025-11-03 01:33:07 | 67167946601 | ALL OTHER | 47.562624 | -122.295275 | POINT (-122.3 47.563) |
| 5 | 2025-321893 | 2025-11-03 02:40:08 | 67168135239 | ALL OTHER | 47.536403 | -122.392255 | POINT (-122.39 47.536) |
| 6 | 2025-321893 | 2025-11-03 02:40:08 | 67168146760 | ALL OTHER | 47.536403 | -122.392255 | POINT (-122.39 47.536) |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 992 | 2025-326543 | 2025-11-06 22:20:01 | 67242117613 | ALL OTHER | 47.601290 | -122.326352 | POINT (-122.33 47.601) |
| 993 | 2025-326543 | 2025-11-06 22:20:01 | 67242080867 | ALL OTHER | 47.601290 | -122.326352 | POINT (-122.33 47.601) |
| 994 | 2025-326526 | 2025-11-06 22:20:45 | 67242088334 | PROPERTY CRIME | 47.668662 | -122.369682 | POINT (-122.37 47.669) |
| 995 | 2025-326592 | 2025-11-06 22:51:55 | 67242211063 | ALL OTHER | 47.612939 | -122.344783 | POINT (-122.34 47.613) |
| 996 | 2025-326576 | 2025-11-06 23:00:37 | 67242247965 | ALL OTHER | 47.696847 | -122.327638 | POINT (-122.33 47.697) |
876 rows × 7 columns
In [19]:
import folium
In [20]:
map = folium.Map(location=[47.6577792,-122.3031197], zoom_start=10)
map
Out[20]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [21]:
map1 = folium.Map(location=[47.6577792,-122.3031197], zoom_start=10)
folium.GeoJson(nbh,
popup=folium.GeoJsonPopup(fields=['L_HOOD'])
).add_to(map1)
map1
Out[21]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [22]:
map1.save("index.html")
In [23]:
map2 = folium.Map(location=[47.6577792,-122.3031197], zoom_start=10)
folium.GeoJson(beats,
popup=folium.GeoJsonPopup(fields=['beat'])
).add_to(map2)
map2
Out[23]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [24]:
spd_crime3['report_date_time'] = spd_crime3['report_date_time'].dt.strftime('%Y-%m-%d %H:%M:%S')
map3 = folium.Map(location=[47.6577792,-122.3031197], zoom_start=10)
folium.GeoJson(spd_crime3).add_to(map3)
map3
c:\Users\turne\anaconda3\Lib\site-packages\geopandas\geodataframe.py:1968: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy super().__setitem__(key, value)
Out[24]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [25]:
offense_type = spd_crime3.offense_category.unique().tolist()
offense_type
Out[25]:
['ALL OTHER', 'VIOLENT CRIME', 'PROPERTY CRIME']
In [26]:
colors = {'PROPERTY CRIME':'red', 'VIOLENT CRIME': 'darkpurple', 'ALL OTHER':'lightblue'}
In [27]:
map4 = folium.Map(location=[47.6577792,-122.3031197], zoom_start=10)
# Add markers with colors based on category
for index, row in spd_crime3.iterrows():
folium.Marker(
location=[row['latitude'], row['longitude']],
popup=row['offense_category'],
icon=folium.Icon(color=colors.get(row['offense_category'], 'gray')) # Default to gray if category not found
).add_to(map4)
map4
Out[27]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [28]:
curr_date = pd.to_datetime('today', utc=True).tz_convert('America/Los_Angeles').date()
curr_date
Out[28]:
datetime.date(2025, 11, 7)
In [29]:
incidents_today = spd_crime3[pd.to_datetime(spd_crime3['report_date_time']).dt.date == curr_date]
incidents_today
Out[29]:
| report_number | report_date_time | offense_id | offense_category | latitude | longitude | geometry |
|---|
In [30]:
map5 = folium.Map(location=[47.6577792,-122.3031197], zoom_start=10)
folium.GeoJson(incidents_today).add_to(map5)
map5
Out[30]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [31]:
incidents_yesterday = spd_crime3[pd.to_datetime(spd_crime3['report_date_time']).dt.date == curr_date - pd.Timedelta(days=1)]
incidents_yesterday
Out[31]:
| report_number | report_date_time | offense_id | offense_category | latitude | longitude | geometry | |
|---|---|---|---|---|---|---|---|
| 854 | 2025-325470 | 2025-11-06 00:15:06 | 67217550137 | ALL OTHER | 47.632927 | -122.361016 | POINT (-122.36 47.633) |
| 855 | 2025-325470 | 2025-11-06 00:15:06 | 67217554931 | ALL OTHER | 47.632927 | -122.361016 | POINT (-122.36 47.633) |
| 856 | 2025-325470 | 2025-11-06 00:15:06 | 67217557249 | ALL OTHER | 47.632927 | -122.361016 | POINT (-122.36 47.633) |
| 857 | 2025-325482 | 2025-11-06 00:55:17 | 67217736640 | ALL OTHER | 47.595837 | -122.326374 | POINT (-122.33 47.596) |
| 858 | 2025-325479 | 2025-11-06 01:30:41 | 67217866391 | ALL OTHER | 47.509647 | -122.265438 | POINT (-122.27 47.51) |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 992 | 2025-326543 | 2025-11-06 22:20:01 | 67242117613 | ALL OTHER | 47.601290 | -122.326352 | POINT (-122.33 47.601) |
| 993 | 2025-326543 | 2025-11-06 22:20:01 | 67242080867 | ALL OTHER | 47.601290 | -122.326352 | POINT (-122.33 47.601) |
| 994 | 2025-326526 | 2025-11-06 22:20:45 | 67242088334 | PROPERTY CRIME | 47.668662 | -122.369682 | POINT (-122.37 47.669) |
| 995 | 2025-326592 | 2025-11-06 22:51:55 | 67242211063 | ALL OTHER | 47.612939 | -122.344783 | POINT (-122.34 47.613) |
| 996 | 2025-326576 | 2025-11-06 23:00:37 | 67242247965 | ALL OTHER | 47.696847 | -122.327638 | POINT (-122.33 47.697) |
125 rows × 7 columns
In [32]:
map6 = folium.Map(location=[47.6577792,-122.3031197], zoom_start=10)
folium.GeoJson(incidents_yesterday,
popup=folium.GeoJsonPopup(fields=['offense_category'])
).add_to(map6)
map6
Out[32]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [33]:
from datetime import date
# the number of crime on mmddyy at the Beat U2, replace mmddyy to a date in your dataset
crime_251106=spd_crime3[pd.to_datetime(spd_crime3['report_date_time']).dt.date == date(2025, 11, 6)]
beat_u2=beats[beats['beat']=='U2']
crime_u2 = crime_251106.overlay(beat_u2, how='intersection')
map7 = folium.Map(location=[47.6577792,-122.3031197], zoom_start=10)
folium.GeoJson(crime_u2,
popup=folium.GeoJsonPopup(fields=['report_date_time','offense_category'])
).add_to(map7)
map7
Out[33]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [34]:
spd_crime3['date'] = pd.to_datetime(spd_crime3['report_date_time']).dt.date
daily_crime_counts = spd_crime3.groupby(['date'])['offense_id'].count().reset_index()
daily_crime_counts
c:\Users\turne\anaconda3\Lib\site-packages\geopandas\geodataframe.py:1968: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy super().__setitem__(key, value)
Out[34]:
| date | offense_id | |
|---|---|---|
| 0 | 2025-11-03 | 283 |
| 1 | 2025-11-04 | 271 |
| 2 | 2025-11-05 | 197 |
| 3 | 2025-11-06 | 125 |
In [35]:
daily_crime_counts = daily_crime_counts.rename(columns={"offense_id":"n_incidents"})
daily_crime_counts.plot.bar(x='date', y='n_incidents')
Out[35]:
<Axes: xlabel='date'>
In [36]:
crime_110325=spd_crime3[pd.to_datetime(spd_crime3['report_date_time']).dt.date == date(2025, 11, 3)]
crime_nbh_110325 = nbh.sjoin(crime_110325, how='left', predicate='intersects')
crime_nbh_110325 = crime_nbh_110325.dissolve(by='S_HOOD', aggfunc='count').reset_index()
In [37]:
crime_nbh_110325 = crime_nbh_110325[['S_HOOD','geometry','OBJECTID']].rename(columns={"OBJECTID":"n_incidents"})
crime_110325
Out[37]:
| report_number | report_date_time | offense_id | offense_category | latitude | longitude | geometry | date | |
|---|---|---|---|---|---|---|---|---|
| 2 | 2025-321994 | 2025-11-03 01:05:38 | 67167876122 | ALL OTHER | 47.703422 | -122.344675 | POINT (-122.34 47.703) | 2025-11-03 |
| 3 | 2025-321990 | 2025-11-03 01:12:58 | 67167892721 | VIOLENT CRIME | 47.711198 | -122.344795 | POINT (-122.34 47.711) | 2025-11-03 |
| 4 | 2025-321981 | 2025-11-03 01:33:07 | 67167946601 | ALL OTHER | 47.562624 | -122.295275 | POINT (-122.3 47.563) | 2025-11-03 |
| 5 | 2025-321893 | 2025-11-03 02:40:08 | 67168135239 | ALL OTHER | 47.536403 | -122.392255 | POINT (-122.39 47.536) | 2025-11-03 |
| 6 | 2025-321893 | 2025-11-03 02:40:08 | 67168146760 | ALL OTHER | 47.536403 | -122.392255 | POINT (-122.39 47.536) | 2025-11-03 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 309 | 2025-322878 | 2025-11-03 22:54:04 | 67181560704 | ALL OTHER | 47.667785 | -122.281543 | POINT (-122.28 47.668) | 2025-11-03 |
| 310 | 2025-322929 | 2025-11-03 22:59:55 | 67183503429 | ALL OTHER | 47.692091 | -122.302392 | POINT (-122.3 47.692) | 2025-11-03 |
| 311 | 2025-322929 | 2025-11-03 22:59:55 | 67181595406 | PROPERTY CRIME | 47.700722 | -122.302935 | POINT (-122.3 47.701) | 2025-11-03 |
| 312 | 2025-323073 | 2025-11-03 23:45:34 | 67182749478 | PROPERTY CRIME | 47.622595 | -122.312637 | POINT (-122.31 47.623) | 2025-11-03 |
| 313 | 2025-323039 | 2025-11-03 23:49:37 | 67182851532 | PROPERTY CRIME | 47.616987 | -122.320826 | POINT (-122.32 47.617) | 2025-11-03 |
283 rows × 8 columns
In [38]:
from branca.colormap import linear
colormap = linear.PuRd_05.scale(
crime_nbh_110325.n_incidents.min(), crime_nbh_110325.n_incidents.max()
)
data_dict = crime_nbh_110325.set_index("S_HOOD")["n_incidents"]
map8 = folium.Map(location=[47.6577792,-122.3031197], zoom_start=11)
folium.GeoJson(crime_nbh_110325,
name="incidents",
style_function=lambda feature: {
"fillColor": colormap(data_dict[feature['properties']["S_HOOD"]]),
"color": "black",
"weight": 1,
"dashArray": "5, 5",
"fillOpacity": 0.9,
},
popup=folium.GeoJsonPopup(fields=['S_HOOD','n_incidents'])
).add_to(map8)
folium.LayerControl().add_to(map8)
colormap.add_to(map8)
map8
Out[38]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [39]:
from shapely.geometry import Point
ps_loc = Point(-122.3313539, 47.6027217)
ps = gpd.GeoDataFrame(geometry=[ps_loc], crs=4326)
ps_utm = ps.to_crs('epsg:32610')
ps_buffer = ps_utm.buffer(500)
ps_buffer_4326 = ps_buffer.to_crs('epsg:4326')
ps_buffer_4326
Out[39]:
0 POLYGON ((-122.32 47.603, -122.32 47.602, -122... dtype: geometry
In [40]:
map9 = folium.Map(location=[47.6008863,-122.3377], zoom_start=14)
folium.GeoJson(
data=ps_buffer_4326.to_json(),
).add_to(map9)
folium.Marker(
location=[47.6027217,-122.3313539],
).add_to(map9)
map9
Out[40]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [41]:
ps_buffer_4326 = gpd.GeoDataFrame(geometry=ps_buffer_4326)
In [42]:
crime_ps = spd_crime3.overlay(ps_buffer_4326, how='intersection')
In [43]:
crime_ps['date'] = crime_ps['date'].astype(str)
map10 = folium.Map(location=[47.6008863,-122.3377], zoom_start=14)
folium.GeoJson(crime_ps,
popup=folium.GeoJsonPopup(fields=['offense_category']) # turn on the popop to display the offense category.
).add_to(map10)
map10
Out[43]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [44]:
import ipywidgets as widgets
from IPython.display import display
spd_crime3['report_date_time'] = spd_crime3['report_date_time'].astype('str')
spd_crime3['date'] = spd_crime3['date'].astype('str')
map11 = folium.Map(location=[47.6008863,-122.3377], zoom_start=10)
out = widgets.Output(layout={'border': '1px solid black'})
# Create a Dropdown widget
dropdown = widgets.Dropdown(
options=nbh['S_HOOD'].values.tolist(),
value=nbh['S_HOOD'].values[0], # Default selected value
description='Select',
disabled=False
)
def on_dropdown_change(change):
out.clear_output()
with out:
map11_n = folium.Map(location=[47.6008863,-122.3377], zoom_start=10)
my_gdf = spd_crime3.overlay(nbh[nbh['S_HOOD']==dropdown.value], how='intersection')
layer1 = folium.GeoJson(my_gdf).add_to(map11_n)
n_mygdf = len(my_gdf)
print ("The number of crime at "+ dropdown.value + f" is {n_mygdf}")
display(map11_n)
dropdown.observe(on_dropdown_change, names='value')
display(dropdown)
with out:
display(map11)
out
c:\Users\turne\anaconda3\Lib\site-packages\geopandas\geodataframe.py:1968: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy super().__setitem__(key, value) c:\Users\turne\anaconda3\Lib\site-packages\geopandas\geodataframe.py:1968: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy super().__setitem__(key, value)
Dropdown(description='Select', options=('Loyal Heights', 'Ballard', 'Whittier Heights', 'West Woodland', 'Phin…
Output(layout=Layout(border='1px solid black'))
In [45]:
spd_crime3['report_date_time'] = pd.to_datetime(spd_crime3['report_date_time']).sort_values(ascending=True).dt.date
data = []
for _, d in spd_crime3.groupby('report_date_time'):
data.append([[row['latitude'], row['longitude']] for _, row in d.iterrows()])
c:\Users\turne\anaconda3\Lib\site-packages\geopandas\geodataframe.py:1968: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy super().__setitem__(key, value)
In [46]:
from datetime import datetime, timedelta
time_index = [
(min(spd_crime3['report_date_time']) + k * timedelta(1)).strftime("%Y-%m-%d") for k in range(len(data))
]
In [47]:
from folium.plugins import HeatMapWithTime
map12 = folium.Map(location=[47.6008863,-122.3377], zoom_start=10)
hm = folium.plugins.HeatMapWithTime(data, index=time_index, auto_play=True, max_opacity=0.3)
hm.add_to(map12)
display(map12)
Make this Notebook Trusted to load map: File -> Trust Notebook